'use strict';

app.controller('demoCtrl', ['$scope', '$http', '$stateParams', '$rootScope', '$modal', 'lodash','$location','$anchorScroll', function ($scope, $http, $stateParams, $rootScope, $modal, lodash,$location,$anchorScroll) {
    $rootScope.initBreadcrumb($scope.breadcrumb);
    //页面加载时，给headerElement设置元素
    $rootScope.initHeaderElements($scope.settings);
    var debunker = {
        show: true,
        label: "吐槽",
        func: function () {
            openDebunkerModal();
        }
    }
    var backUrl = {
        show: true,
        label: "返回",
        func: function () {
            $rootScope.backHistory();
        }
    }
    if($rootScope.isUserCanOperateByRole('pdm')){
        $scope.settings.btnEle.push(debunker);
    }
    $scope.settings.btnEle.push(backUrl);
    
    $scope.info = {
        reqId : $stateParams.reqId,
        reqDetails: "",
    };
    
    getReqName();

    //获取req名称
    function getReqName() {
        var url = matrix_product_url +"/brdRequirement/getBrdRequirements?withFeatureDesc=0&reqId="+ $stateParams.reqId;
        $http.get(url).success(function(result) {
            if(result.code == "0") {
                if(result.data) {
                    $scope.info.reqDetails = result.data[0];
                    $scope.breadcrumb.push('产品设计',$scope.info.reqDetails.reqName);
                }
            } else {
                $rootScope.toasterWarningInfo(result.desc);
            }
        });
    }

    //  打开吐槽modal
    function openDebunkerModal() {
        var modalInstance = $modal.open({
            animation: true,
            templateUrl: getTemplateUrl("matrix/component/tpl/addDebunker.html"),
            controller: "addDebunkerCtrl",
            keyboard: true,
            backdrop: true
        });

        modalInstance.result.then(function (debunkerDetails) {
            addDebunker(debunkerDetails);
        }, function (reason) {
            console.log("关闭modal");
        });
    }

    // 添加吐槽
    function addDebunker(debunkerDetails) {
        var postParams = {
            "createrAccount": getUserAccount() || "",
            "createrRealname": getUserName() || "",
            "debunkDesc": debunkerDetails.debunkerData,
            "debunkerAccount": debunkerDetails.userItem.account || "",
            "debunkerRealname": debunkerDetails.userItem.realname || "",
            "isImprove": "1",
            "reqId": $stateParams.reqId
        };
        var url = matrix_product_url +"/debunk/save";
        $http({
            method: "post",
            url: url,
            data: JSON.stringify(postParams),
            headers: {"Content-Type": "application/json"}
        }).success(function (result) {
            if (result.code == "0") {
                $rootScope.toasterSuccessInfo("添加成功！");
            } else {
                $rootScope.toasterErrorInfo("添加失败，请刷新重试！");
            }
        });
    }
}]);
app.controller("addDebunkerCtrl", ["$scope", "$rootScope", "$modalInstance", "$http", "lodash", function ($scope, $rootScope, $modalInstance, $http, lodash) {
    $scope.userList = [];
    $scope.sltUserItem = "";
    $scope.debunkerData = "";

    getAllUser();

    // 获取全部用户
    function getAllUser() {
        var url = beetle_auth_url + "/user/?token_id=" + getTokenId();
        $http.get(url).success(function(result) {
            if (result.success) {
                if (result.data) {
                    angular.forEach(result.data, function(user) {
                        user.crtname = user.realname;
                    });
                    $scope.userList = result.data;
                } else {
                    $rootScope.toasterErrorInfo("获取用户数据错误！");
                }
            } else {
                $rootScope.toasterErrorInfo(result.msg);
            }
        });
    }

    // 选择用户
    $scope.selectUserFun = function(item) {
        $scope.sltUserItem = item;
    }

    $scope.createClick = function () {
        if(!$scope.sltUserItem) {
            $rootScope.toasterWarningInfo("请选择提出人！");
            return false;
        }
        if(!$scope.debunkerData) {
            $rootScope.toasterErrorInfo("内容不能为空！");
            return false;
        }
        var debunkerDetails = {
            "userItem": $scope.sltUserItem,
            "debunkerData": $scope.debunkerData
        };

        $modalInstance.close(debunkerDetails);
    };

    $scope.cancelClick = function () {
        $modalInstance.dismiss("cancel");
    };
}]);